package ServerNetwork
{
	import Interfaces.MIAppStarter;
	
	import XMFramework.Interface.IFramework;

	public class ServerFunc
	{
		private static function getSessionParam( coreSNSFramework : IFramework ):Array
		{
			var root:MIAppStarter	=	coreSNSFramework.globalHolder.getVariable( ShareMacros.VAR_APP_ROOT );
			return root.packSessionData();
		}
		
		public static function requestInitGameApp( resFunc : Function, coreSNSFramework : IFramework) : void
		{
			var params : Array	=	new Array();
			
			var msgID : int		=	coreSNSFramework.configData.getMetadata( ShareMacros.CONF_MESSAGE_TYPE )[ShareMessages.MSG_getGameInitialization];
			
			var callId : int	=	coreSNSFramework.networkSystem.overrideBucket( getSessionParam( coreSNSFramework ), resFunc, msgID, params, ShareConstant.NET_PRIORITY_IMMEDIATELY );
			
			ApplicationLog.networkRequestLog( msgID, callId, params );
		}
		
		public static function loadFriendlist( coreSNSFramework : IFramework, callbackFunc : Function ):void
		{
			var params : Array		=	new Array();
			
			params['signInUserId']	=	"23234234";
			params['userId']		=	"12312312";
			params['sessionId']		=	"2342312";
			params['friendUserId']	=	"0";
						
			//var _testURL : String		=	coreSNSFramework.configData.getMetadata( ShareMacros.CONF_NETWORK )["gateway"];
			var _testURL : String		=	"http://127.0.0.1:8025/v2/logic";
			var _testService : String	=	"Service";
			coreSNSFramework.networkSystem.callService( _testURL, "Service", "getFriendList", callbackFunc, params );
			
			/*var testData:Object	=	
				{
					friends:
					[
						{ exp:2222, username:"uf1", name:"F1", gold:411 },
						{ exp:3333, username:"uf2", name:"F2", gold:422 },
						{ exp:4444, username:"uf3", name:"F3", gold:433 }
					]
				};
			
			callbackFunc( 0, testData );*/
		}
		
		public static function loadInitGame( coreSNSFramework : IFramework, callbackFunc : Function ):void
		{
			var params : Array		=	new Array();
			
			params['signInUserId']	=	"23234234";
			params['userId']		=	"12312312";
			params['sessionId']		=	"2342312";
			params['friendUserId']	=	"0";
			
			//var _testURL : String		=	coreSNSFramework.configData.getMetadata( ShareMacros.CONF_NETWORK )["gateway"];
			var _testURL : String		=	"http://127.0.0.1:8025/v2/logic";
			var _testService : String	=	"Service";
			coreSNSFramework.networkSystem.callService( _testURL, _testService, "loadGame", callbackFunc, params );
			
			/*var testData:Object	=
				{
					exp:3244,
					coin:2566,
					name:"linh",
					gold:3111,
					cotes:
					{
						2:
						{
							penguins:
							[
								{id:1, type:1001, level:1, exp:100},
								{id:2, type:1001, level:2, exp:101},
								{id:3, type:1001, level:1, exp:130}
							]
						}
					}
				};
			
			callbackFunc( 0, testData );*/
		}
		
		public static function visitFriend( coreSNSFramework : IFramework, callbackFunc : Function, friendId : String ):void
		{
			var testData:Object	=	
				{
					friendInfo:
					{
						username:"Bạn dummy",
						exp:69,
						energy:69,
						coin:69,
						gold:69,
						level:2
					},
					cage:
					{
						backgroundId:1,
						penguins:
						[
							{id:1, type:1001, level:1, exp:100},
							{id:2, type:1001, level:2, exp:101},
							{id:3, type:1001, level:1, exp:130}
						]
					},
					cagelist:
					{
						cages:
						[
							{type:1001},
							{type:1002},
							{type:1003}
						]
					}
				};
			
			callbackFunc( 0, testData );
		}
	
		public static function onChangeCage( coreSNSFramework : IFramework, callbackFunc : Function ):void
		{
			var testData:Object = 
				{
					backgroundId:1,
					penguins:
					[
						{id:1, type:1001, level:1, exp:100},
						{id:2, type:1001, level:2, exp:101},
						{id:3, type:1001, level:1, exp:130}
					]
				};
			callbackFunc( 0, testData );
		}

		public static function onBuyPenguins( type:Object ):String // Trả về id của con penguins mới mua
		{
			// Dữ liệu trả về là một chuỗi id giống với dữ liệu test
			return String(Math.floor(Math.random()*100));
		}
		
		public static function onDestroyPenguins( id:Object ):void
		{
			// gọi server hủy con penguins với cái id
		}
	}	
}